package com.gxc.webcore.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

public class FileUtil {

	/**
	 * 遍历dir目录下所有以fileEnd结尾的文件，并返回文件的完整路径
	 * 
	 * @param dir
	 * @param fileEnd
	 * @return
	 */
	public static List<String> listFilesEndWith(String dir, String fileEnd) {
		ArrayList<String> list = new ArrayList<String>();
		File root = new File(dir);
		File[] files = root.listFiles();
		for (File file : files) {
			String name = file.getName();
			if (file.isDirectory()) {
				List<String> list2 = listFilesEndWith(dir + "/" + name, fileEnd);
				list.addAll(list2);
			} else {
				if (name.endsWith(fileEnd)) {
					list.add(dir + File.separator + name);
				}
			}
		}
		return list;
	}

	/**
	 * 遍历dir目录下所有以fileStart开头的文件，并返回文件的完整路径
	 * 
	 * @param dir
	 * @param fileStart
	 * @return
	 */
	public static List<String> listFilesStartWith(String dir, String fileStart) {
		ArrayList<String> list = new ArrayList<String>();
		File root = new File(dir);
		File[] files = root.listFiles();
		for (File file : files) {
			String name = file.getName();
			if (file.isDirectory()) {
				List<String> list2 = listFilesStartWith(dir + "/" + name,
						fileStart);
				list.addAll(list2);
			} else {
				if (name.startsWith(fileStart)) {
					list.add(dir + File.separator + name);
				}
			}
		}
		return list;
	}

	/**
	 * 按时间升序排列文件
	 * 
	 * @param files
	 * @return
	 */
	public static List<File> sortUp(File[] files) {
		return sortUp(Arrays.asList(files));
	}

	/**
	 * 按时间升序排列文件
	 * 
	 * @param files
	 * @return
	 */
	public static List<File> sortUp(List<File> files) {
		List<File> list = sortDown(files);
		if (list != null) {
			Collections.reverse(list);
		}
		return list;
	}

	/**
	 * 按时间降序排列文件
	 * 
	 * @param files
	 * @return
	 */
	public static List<File> sortDown(File[] files) {
		return sortDown(Arrays.asList(files));
	}

	/**
	 * 按时间降序排列文件
	 * 
	 * @param files
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<File> sortDown(List<File> files) {
		TreeMap tm = new TreeMap();
		int fileNum = files.size();
		for (int i = 0; i < fileNum; i++) {
			Long tempLong = new Long(files.get(i).lastModified());
			if (tm.get(tempLong) != null) {
				if (tm.get(tempLong) instanceof List) {
					List tempList = (List) tm.get(tempLong);
					tempList.add(files.get(i));
					tm.put(tempLong, tempList);
				} else {
					List tempList = new ArrayList();
					tempList.add(tm.get(tempLong));
					tempList.add(files.get(i));
					tm.put(tempLong, tempList);
				}
			} else {
				tm.put(tempLong, files.get(i));
			}
		}
		List<File> list = new ArrayList<File>();
		Set<Long> set = tm.keySet();
		Iterator<Long> it = set.iterator();
		Object obj = null;
		while (it.hasNext()) {
			obj = it.next();
			if (tm.get(obj) instanceof List) {
				list.addAll((List<File>) tm.get(obj));
			} else {
				list.add((File) tm.get(obj));
			}
		}
		return list;
	}

}
